package com.css.fxfzypg.zcpt.sys.service.impl;

import com.css.fxfzypg.zcpt.sys.entity.SUser;
import com.css.fxfzypg.zcpt.sys.entity.vo.UpdatePasswordVO;
import com.css.fxfzypg.zcpt.sys.service.SUserService;
import com.css.fxfzypg.base.user.viewobjects.UserRegisterVO;
import com.css.fxfzypg.util.*;
import com.css.util.Md5Util;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Service
public class SUserServiceImpl implements SUserService {

    @Autowired
    private  RestTemplateUtil restTemplateUtil;

    @Override
    public ResponseEntity<String> addSuser(UserRegisterVO sUser) throws Exception {
        //读取配置文件
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/addOrUpdateSUser";
        Map<String, Object> paramMap = new HashMap<>(1);
//        paramMap.put("user", sUser);
        paramMap.put("userName",sUser.getUserName());
        paramMap.put("password", sUser.getPassword());
        paramMap.put("mobile",sUser.getMobile());
        paramMap.put("realName",sUser.getRealName());
        paramMap.put("certNo",sUser.getCertNo());
        paramMap.put("orgId",sUser.getOrgId());
        ResponseEntity<String> stringResponseEntity = restTemplateUtil.sendPostHttpRequest(url, paramMap);
        return stringResponseEntity;
    }
    /**
     * 获取用户信息
     */
    @Override
    public String getSUser(String userId) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/getUser?userId="+userId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("userId",userId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public String queryUsersByOrg(String orgId) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/queryUsersByOrg?orgId="+orgId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("orgId",orgId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public String queryUsersByOrg1(String orgId, String userName, Integer pageSize, Integer currentPage) throws UnsupportedEncodingException {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/pageUsersByOrgAndUserName?orgId="+orgId+"&userName="+ URLEncoder.encode(userName,"UTF-8") +"&pageSize="+pageSize+"&currentPage="+currentPage;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("orgId",orgId);
        paramMap.put("userName",URLEncoder.encode(userName,"UTF-8"));
        paramMap.put("pageSize",pageSize);
        paramMap.put("currentPage",currentPage);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public String getGroupUserInfo(String userId) {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/getGroupUserInfo?userId="+userId;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("userId",userId);
        ResponseEntity<String> responseEntity = restTemplateUtil.sendHttpRequest(url, paramMap);
        return  responseEntity.getBody();
    }

    @Override
    public String updateUserPassword(UpdatePasswordVO vo) throws Exception {
        //读取配置文件
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("baseRestUrl") + "/suser_dzj/updUserPassword";
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("userId",vo.getUserId());
        paramMap.put("oldPassword", Md5Util.getMD5(vo.getOldPassword()));
        paramMap.put("password",Md5Util.getMD5(vo.getPassword()));
        paramMap.put("salt","");
        ResponseEntity<String> stringResponseEntity = restTemplateUtil.sendPostHttpRequest(url, paramMap);
        return stringResponseEntity.getBody();
    }
}
